﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace StockStar.GatherData
{
    public class Cache
    {
        static ICacheManager cachemanager;

        static Cache()
        {
            cachemanager = CacheFactory.GetCacheManager();
        }

        /// <summary>
        /// 缓存时间
        /// </summary>
        /// <param name="cachesecond"></param>
        /// <returns></returns>
        public static DateTime calculateCacheTime(int cachesecond)
        {
            int second = System.DateTime.Now.Second + System.DateTime.Now.Minute * 60;
            int hour = System.DateTime.Now.Hour;
            DateTime dt = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd"));
            second = second + (cachesecond - second % cachesecond);
            dt = dt.AddHours(hour).AddSeconds(second);
            return dt;
        }

        /// <summary>
        /// 存储缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool addCache(string key, object obj)
        {
            try
            {
                DateTime nextTime = calculateCacheTime(53);
                cachemanager.Add(key, obj, CacheItemPriority.Normal, null, new AbsoluteTime(nextTime));
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 存储缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool addCache(string key, object obj, int seconds)
        {
            try
            {
                DateTime nextTime = calculateCacheTime(seconds);
                cachemanager.Add(key, obj, CacheItemPriority.Normal, null, new AbsoluteTime(nextTime));
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 从缓存中检索数据
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object getCache(string key)
        {
            try
            {
                cachemanager = CacheFactory.GetCacheManager();
                return cachemanager.GetData(key);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 从缓存中移除数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object MoveCache(string key)
        {
            try
            {
                cachemanager = CacheFactory.GetCacheManager();
                cachemanager.Remove(key);
                return true;
            }
            catch
            {
                return null;
            }
        }
    }

    public static class CacheKey
    {
        public static string News_Cache_List = "News_Cache_List";
    }
}
